package com.thrd.taiexhibition.bean

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.WindowManager
import com.alibaba.android.arouter.launcher.ARouter
import com.thrd.basicframework.swipeback.SwipeBackActivity
import com.zackratos.ultimatebarx.library.UltimateBarX
import com.zackratos.ultimatebarx.library.bean.BarConfig

/**
 * @author: ${唐杰}
 * @date: 2021/2/25
 * @data: 支持侧滑返回的Activity
 */
open abstract  class BackActivity : SwipeBackActivity() {
    private val handler: Handler=Handler(Looper.getMainLooper());

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //初始化跳转
        ARouter.getInstance().inject(this)
        //initTitle()
        fullScreen(this)
        window.decorView
            .setOnSystemUiVisibilityChangeListener { visibility ->
                if (visibility == View.VISIBLE) {
                    handler.postDelayed(Runnable { fullScreen(this) }, 1000)
                }
            }
    }

    /**
     * 初始化界面标题
     */
    private fun initTitle(){
        //隐藏标题
        supportActionBar?.hide()
        val config = BarConfig.newInstance()          // 创建配置对象
            .fitWindow(false)                          // 布局是否侵入状态栏（true 不侵入，false 侵入）
            .color(Color.RED)                         // 状态栏背景颜色（色值）
            .colorRes(Color.TRANSPARENT)            // 状态栏背景颜色（资源id）
//            .drawableRes(R.drawable.bg_gradient)      // 状态栏背景 drawable
            .light(false)
        UltimateBarX.with(this)                       // 对当前 Activity 或 Fragment 生效
            .config(config)                           // 使用配置
            .applyStatusBar()
    }

    /**
     * 通过设置全屏，设置状态栏透明
     *
     * @param activity
     */
    private  fun fullScreen(activity: Activity) {
        supportActionBar?.hide()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.x开始需要把颜色设置透明，否则导航栏会呈现系统默认的浅灰色
                val window = activity.window
                val decorView = window.decorView
                //两个 flag 要结合使用，表示让应用的主体内容占用系统状态栏的空间
                val option = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or  //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or  //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or  //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
                //   |View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                decorView.systemUiVisibility = option
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                val flagTranslucentNavigation =
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
                window.addFlags(flagTranslucentNavigation)
                window.statusBarColor = Color.TRANSPARENT
                //导航栏颜色也可以正常设置
//                window.setNavigationBarColor(Color.TRANSPARENT);
            } else {
                val window = activity.window
                val attributes = window.attributes
                val flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                val flagTranslucentNavigation =
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
                window.addFlags(flagTranslucentNavigation)
                attributes.flags = attributes.flags or flagTranslucentStatus
                //                attributes.flags |= flagTranslucentNavigation;
                window.attributes = attributes
            }
        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
//            getWindow().getDecorView().setSystemUiVisibility(uiOptions); // 使用activity的window是隐藏虚拟按键。
//           // getWindow().setNavigationBarColor(Color.parseColor("#1bb5d7")); //设置虚拟按键的背景颜色
//            getWindow().setNavigationBarColor(Color.TRANSPARENT);
//        }
    }

}